package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.SystemDictionaryItem;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 借款基本资料逻辑
 */
@Controller
public class BasicInfoController {
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    /**
     * 借款基本资料的完成
     *
     * @param model
     * @return
     */
    @RequestMapping("basicInfo")
    @NeedLogin
    public String basicInfo(Model model) {
        model.addAttribute("userinfo", userInfoService.getById(UserContext.getCurrentUser().getId()));
        /**
         * 个人学历集合
         * 通过数据字典的编号sn 查出明细
         */
        List<SystemDictionaryItem> educationBackgrounds = systemDictionaryService.listBySn("educationBackgrounds");
        model.addAttribute("educationBackgrounds", educationBackgrounds);
        /**
         * 月收入集合
         * 通过数据字典的编号sn 查出明细
         */
        List<SystemDictionaryItem> incomeGrades = systemDictionaryService.listBySn("incomeGrades");
        model.addAttribute("incomeGrades", incomeGrades);
        /**
         * 婚姻状况集合
         * 通过数据字典的编号sn 查出明细
         */
        List<SystemDictionaryItem> marriages = systemDictionaryService.listBySn("marriages");
        model.addAttribute("marriages", marriages);
        /**
         * 子女情况集合
         * 通过数据字典的编号sn 查出明细
         */
        List<SystemDictionaryItem> kidCounts = systemDictionaryService.listBySn("kidCounts");
        model.addAttribute("kidCounts", kidCounts);
        /**
         * 住房条件集合
         * 通过数据字典的编号sn 查出明细
         */
        List<SystemDictionaryItem> houseConditions = systemDictionaryService.listBySn("houseConditions");
        model.addAttribute("houseConditions", houseConditions);
        return "borrow/userInfo";
    }

    /**
     * 修改个人信息
     *
     * @param userInfo
     * @return
     */
    @RequestMapping("basicInfo_save")
    @ResponseBody
    public JsonResult basicInfo_save(UserInfo userInfo) {
        JsonResult jsonResult = new JsonResult();
        try {
            userInfoService.basicInfo_save(userInfo);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }

}
